/* CoralCube | Massively Multiplayer Online Role-Playing Game(MMORPG) Emulator.
|* 
|* Copyright (C) 2008-2010 CoralCube <http://www.CoralCube.com/>
|*
|* This program is free software. You can redistribute it and/or modify it under the terms of 
|* the GNU General Public License as published by the Free Software Foundation, either 
|* version 2 of the License.
|*
|* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY, 
|* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
|* See the GNU General Public License for more details.
|*
|* You should have received a copy of the GNU General Public License along with this program. 
|* If not, see <http://www.gnu.org/licenses/>.
|*/

#ifndef _CONFIGDATABASE_H
#define _CONFIGDATABASE_H

#include "DatabaseWorkerPool.h"
#include "MySQLConnection.h"

/// ConfigDatabase Configuration elements
enum ConfigDatabaseBool
{
    CONFIG_AUTH_PROCESS_PRIORITY,
    CONFIG_AUTH_WRONG_PASS_BAN_TYPE,
    CONFIG_LOG_CONFIG_TIMESTAMP,
    CONFIG_LOG_DB_ENABLE,
    CONFIG_LOG_FILE_TIMESTAMP,
    CONFIG_LOG_SQL_TIMESTAMP,
    CONFIG_WORLD_LOG_ARENA_TIMESTAMP,
    CONFIG_WORLD_LOG_CHAR_DUMP,
    CONFIG_WORLD_LOG_CHAR_DUMP_SEPARATE,
    CONFIG_WORLD_LOG_CHAR_TIMESTAMP,
    CONFIG_WORLD_LOG_CHAT_TIMESTAMP,
    CONFIG_WORLD_LOG_DB_CHAR,
    CONFIG_WORLD_LOG_DB_CHAT,
    CONFIG_WORLD_LOG_DB_GM,
    CONFIG_WORLD_LOG_DB_RA,
    CONFIG_WORLD_LOG_DB_TIMESTAMP,
    CONFIG_WORLD_LOG_DB_WORLD,
    CONFIG_WORLD_LOG_FILTER_ACHIEVEMENT_UPDATES,
    CONFIG_WORLD_LOG_FILTER_CREATURE_MOVES,
    CONFIG_WORLD_LOG_FILTER_TRANSPORT_MOVES,
    CONFIG_WORLD_LOG_FILTER_VISIBILITY_CHANGES,
    CONFIG_WORLD_LOG_GM_PER_ACCOUNT,
    CONFIG_WORLD_LOG_GM_TIMESTAMP,
    CONFIG_WORLD_LOG_RA_TIMESTAMP,
    CONFIG_SQLQUERYLOGGING,
    BOOL_CONFIG_DB_VALUE_COUNT
};

enum ConfigDatabaseInt
{
    CONFIG_AUTH_MAX_PING_TIME,
    CONFIG_AUTH_PORT,
    CONFIG_AUTH_REALMS_STATE_UPDATE_DELAY,
    CONFIG_AUTH_USE_PROCESSORS,
    CONFIG_AUTH_WRONG_PASS_BAN_TIME,
    CONFIG_AUTH_WRONG_PASS_MAX_COUNT,
    CONFIG_LOG_CONSOLE_LEVEL,
    CONFIG_LOG_FILE_LEVEL,
    CONFIG_WORLD_LOG_DB_LEVEL,
    CONFIG_WORLD_LOG_FILTER,
    INT_CONFIG_DB_VALUE_COUNT
};

class ConfigDatabaseConnection : public MySQLConnection
{
public:
    //- Constructors for sync and async connections
    ConfigDatabaseConnection(MySQLConnectionInfo& connInfo) : MySQLConnection(connInfo) { }
    ConfigDatabaseConnection(ACE_Activation_Queue* q, MySQLConnectionInfo& connInfo) : MySQLConnection(q, connInfo) {}

    //- Loads databasetype specific prepared statements
    bool Open();
};

typedef DatabaseWorkerPool<ConfigDatabaseConnection> ConfigDatabaseWorkerPool;

enum ConfigDatabaseStatements
{
    /*  Naming standard for defines:
    {DB}_{SET/DEL/ADD/REP}_{Summary of data changed}
    When updating more than one field, consider looking at the calling function
    name for a suiting suffix.
    */

    MAX_CONFIGDATABASE_STATEMENTS,
};

class ConfigDatabaseSettings
{
    friend class ACE_Singleton<ConfigDatabaseSettings, ACE_Null_Mutex>;

public:
    ConfigDatabaseSettings();
    ~ConfigDatabaseSettings();

    std::string pidfile, bindip, logs_dir, log_file, log_world, log_gm, log_char, log_db, log_ra, log_chat, log_arena, log_sql, log_conf, 
        char_dump_dir, console_colors;

    /// Set bool to String for ConfigLog output: '1 (true)' or '0 (false)'
    const char* ToString(bool getBool);
    /// Set String to '<empty>' if this empty for ConfigLog output
    const char* StringEmpty(const char* getString);

    /// Set a server configuration element (see #ConfigDatabaseSettings)
    void setIntConfig(ConfigDatabaseInt index, uint32 value, const char token = ' ');
    uint32 getIntConfig(ConfigDatabaseInt index) const;
    void setBoolConfig(ConfigDatabaseBool index, bool value);
    bool getBoolConfig(ConfigDatabaseBool index) const;

private:
    uint32 c_int_configs[INT_CONFIG_DB_VALUE_COUNT];
    uint32 c_bool_configs[BOOL_CONFIG_DB_VALUE_COUNT];
};

#define sConfigDB (*ACE_Singleton<ConfigDatabaseSettings, ACE_Null_Mutex>::instance())
#endif
